﻿#region Using

using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.ActiveDirectory;
using Rio.Manager.Business.Repository.Translator;
using Rio.Manager.DataAccess;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using NetBay.Core.BusinessEntities.ActiveDirectory.Objects;

#endregion Using

namespace Rio.Manager.Business.Repository
{
    public class ActiveDirectoryRepository
    {
        #region  Get

        /// <summary>Gets the Rio and Ad Correspondence.</summary>
        /// <returns></returns>
        public static List<AdInformationUserRio> GetAllRioADCorrespondence()
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ActiveDirectoryRepository.GetAllRioADCorrespondence (No parameters)...");

            //Variable initialization.
            var l_oReturnLstRioADCorrespondence = new List<AdInformationUserRio>();

            try
            {
                //Get all informations correspondence.
                l_oReturnLstRioADCorrespondence = ActiveDirectoryTranslator.TranslateAllRioADCorrespondence(DA_ActiveDirectory.GetAllRioADCorrespondence());
            }

            catch (Exception ex)
            {
                //Error log.
                NetBay.Core.Log.LogManager.Instance().LogError("ActiveDirectoryRepository.GetAllRioADCorrespondence Exception [" + ex.Message + "]");

                //Return null.
                l_oReturnLstRioADCorrespondence = null;
            }

            //Return value.
            return l_oReturnLstRioADCorrespondence;
        }

        /// <summary>Return a list of mapped object.</summary>
        /// <param name="p_lGroupId">the group identifier from rio</param>
        /// <param name="p_eAdObjectType">The type of object to return</param>
        /// <returns>A list of AdObject</returns>
        public static List<AdObject> GetAdMappedObject(long p_lGroupId, AdObjectType p_eAdObjectType)
        {
            return ActiveDirectoryTranslator.GetAdMappedObject(DA_ActiveDirectory.GetAdMappedObject(p_lGroupId, p_eAdObjectType), p_lGroupId);
        }

        /// <summary>Gets Ad Name mapped.</summary>
        /// <returns></returns>
        public static string GetAdNameMapped()
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ActiveDirectoryRepository.GetAdNameMapped (No parameters)...");

            //Variable initialization.
            string l_sReturnAdNameMapped;

            try
            {
                //Get AD name mapped.
                l_sReturnAdNameMapped = ActiveDirectoryTranslator.TranslateAdNameMapped(DA_ActiveDirectory.GetAdNameMapped());
            }

            catch (Exception ex)
            {
                //Error log.
                NetBay.Core.Log.LogManager.Instance().LogError("ActiveDirectoryRepository.GetAdNameMapped Exception [" + ex.Message + "]");

                //Return null.
                l_sReturnAdNameMapped = null;
            }

            //Return value.
            return l_sReturnAdNameMapped;
        }
        #endregion Get

        #region Add

        /// <summary>Add a user mapping.</summary>
        /// <param name="p_oAdUserToMap">The user Object to map.</param>
        public static bool AddAdUserMapping(AdUser p_oAdUserToMap)
        {
            return DA_ActiveDirectory.AddAdUserMapping(p_oAdUserToMap);
        }
        
        /// <summary>Add a group mapping.</summary>
        /// <param name="p_oAdGroupToMap">The group Object to map.</param>
        public static bool AddAdGroupMapping(AdGroup p_oAdGroupToMap)
        {
            return DA_ActiveDirectory.AddAdGroupMapping(p_oAdGroupToMap);
        }

        #endregion Add

        #region Update

        /// <summary>Update Rio and Ad correspondence.</summary>
        /// <param name="p_oNewValueCorrespondence">Correspondence value to update.</param>
        /// <returns></returns>
        public static bool UpdateRioADCorrespondence(ObservableCollection<AdInformationUserRio> p_oNewValueCorrespondence)
        {
            return DA_ActiveDirectory.UpdateRioADCorrespondence(p_oNewValueCorrespondence);
        }

        #endregion Update

        #region Delete

        /// <summary>Delete a mapped object.</summary>
        /// <param name="p_oAdObjectToDelete">The mapped Object to delete</param>
        public static bool DeleteAdMappedObject(AdObject p_oAdObjectToDelete)
        {
            return DA_ActiveDirectory.DeleteAdMappedObject(p_oAdObjectToDelete);
        }
        
        #endregion
    }
}
